const nav = Pager.nav

const data = [
  {title: '通用', icon: 'icon-riqi', idf: 'normal'},
  {title: '字体', file: '/normal/font', parent: 'normal'}, 
  {title: '图标', file: '/normal/icon', parent: 'normal' },
  {title: '色块', file: '/normal/color', parent: 'normal' },
  {title: '按钮', file: '/normal/button', parent: 'normal' },
  {title: '标签', file: '/normal/tag', parent: 'normal' },
  {title: '行高', file: '/normal/lineheight', parent: 'normal' },
  {title: '返回顶部', file: '/normal/backtop', parent: 'normal' },
  {title: '栅格', file: '/normal/grid', parent: 'normal' },

  {title: '导航', icon: 'icon-riqi', idf: 'navigation'},
  {title: '下拉菜单', file: '/navigation/dropdown', parent: 'navigation'}, 
  {title: '标签页/Sticky', file: '/navigation/tabs', parent: 'navigation'}, 

  {title: '反馈', icon: 'icon-riqi', idf: 'feedback'},
  {title: '文字提示', file: '/feedback/tooltip', parent: 'feedback'}, 
  {title: '警告提示', file: '/feedback/alert', parent: 'feedback'}, 
  {title: '提示框', file: '/feedback/tip', parent: 'feedback'}, 
  {title: '弹出层', file: '/feedback/modal', parent: 'feedback'}, 
  {title: '通知', file: '/feedback/notice', parent: 'feedback'},
  {title: '抽屉', file: '/feedback/drawer', parent: 'feedback'},
  {title: '加载中', file: '/feedback/load', parent: 'feedback'},
  {title: '折叠面板', file: '/feedback/collapse', parent: 'feedback'},
  {title: '步骤条', file: '/feedback/steps', parent: 'feedback'},

  {title: '数据展示', icon: 'icon-riqi', idf: 'show'},
  {title: '树形控件', file: '/show/tree', parent: 'show'}, 
  {title: '表格', file: '/show/table', parent: 'show'}, 
  {title: 'bootstrapTable', file: '/show/btTable', parent: 'show'}, 
  {title: '轮播图', file: '/show/swiper', parent: 'show'}, 
  {title: '图表', file: '/show/echart', parent: 'show'}, 
  {title: '分页', file: '/show/paging', parent: 'show'}, 

  {title: '实际项目', icon: 'icon-riqi', idf: 'items'},
  {title: '列表', file: '/items/order', parent: 'items'}, 
  {title: '登录', file: '/items/login', parent: 'items'}, 
  {title: '缺省页', file: '/items/default', parent: 'items'}, 

  {title: '友情链接', icon: 'icon-riqi', file: '/show/tree'},
]

const adapter = (res) => {
  let path = location.hash.slice(1)
  return res.map(item => {
    let attr = item.file ? {attr: {file: item.file}} : {}
    let idf = !item.parent ? {idf: item.idf} : {}
    let title = item.icon ? {title: <div className='htitle item-icon icon-riqi'><span className='text'>{item.title}</span></div>} : {title: item.title}
    let parent = !item.idf ? {parent: item.parent} : {}
    const xxx = {
      ...title,
      ...attr,
      ...idf,
      ...parent,
      aim: 'aimx',
      itemClass: !item.parent && !item.idf ? 'level0' : '',
      select: item.file && item.file.indexOf(path) > -1 ?  true : false
    }
    return xxx
  })
}
export const routerMenu = ui_tree({
  $$id: 'admin',
  data: adapter(data),
  // header: {
  //   title: 'saui'
  // },
  itemMethod: {
    aimx(e, param, inst){
      let file = inst.attr('file')
      let parent = inst.children.length
      if (parent === 0) {
        inst.siblings().removeClass('active')
        if (!inst.hasClass('.level0')) {
          inst.parent().siblings().forEach(item => {
            item.children.forEach(items => {
              if (items.hasClass('active')) {
                items.removeClass('active')
              }
            })
          })
        }
      }
      inst.toggleClass('active')
      if (file) {
        setTimeout(() => {
          nav.redirectTo({
            url: file,
            beforeNav(to, from, next) {
              next()
            }
          })
        }, 50);
      }
    }
  },
  listClass: 'router-menu-list router-menus',
  ready() {
    routerMenu.children.forEach(item => {
      item.children.forEach(items => {
        if (items.hasClass('active')) {
          item.addClass('active')
        }
      })
    })
    // $('.li-item.active').parents('.level0').addClass('active')
  }
})